package org.example2;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MultiStringServer {
    public static void main(String[] args) throws Exception{

        ServerSocket serverSocket = new ServerSocket(7777);
        System.out.println("-------------服务端已经准备好了-------------");
        while (true) {
            Socket socket = serverSocket.accept();
            System.out.println("socket is "+socket);
            new Thread(new Runnable() {
                @Override
                public void run() {

                    try {
                        InputStream inputStream = socket.getInputStream();
                        System.out.println("inputStream is "+inputStream);
                        while (true) {
                            byte[] buf = new byte[4];
                            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                            int len =0;
                            System.out.println("开始进入while了");
                            while ((len = inputStream.read(buf)) != -1) {
                                System.out.println("进入while了");
                                byteArrayOutputStream.write(buf, 0, len);
                                byteArrayOutputStream.flush();
                            }

                            byte[] bytes = byteArrayOutputStream.toByteArray();
                            String result = new String(bytes);
                            System.out.println("客户端发送的是:" + result);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }




    }
}
